//
//  Bow.cpp
//  Archer
//
//  Created by Wu Wang on 12-4-23.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#include "Bow.h"
#include <cmath>

CSpriteBow *CSpriteBow::spriteWithFile(const char *fileName)
{
    CSpriteBow *popSprite = new CSpriteBow;
    if((NULL != popSprite))
    {
        popSprite->initWithFile(fileName);
        popSprite->autorelease();
        return popSprite;
    }
    
    CC_SAFE_DELETE(popSprite);
    return NULL;
}

CSpriteBow *CSpriteBow::spriteWithSpriteFrameName(const char *fileName)
{
    CSpriteBow *popSprite = new CSpriteBow;
    if((NULL != popSprite))
    {
        popSprite->initWithSpriteFrameName(fileName);
        popSprite->autorelease();
        return popSprite;
    }
    
    CC_SAFE_DELETE(popSprite);
    return NULL;

}

void CSpriteBow::draw()
{
    TestBack();
    glColor4f(0.0, 0.0, 0.0, 1.0);
    glLineWidth(3.0f);
    ccDrawLine(m_ptUp, m_ptCenter);
    ccDrawLine(m_ptCenter, m_ptDown);

    CCSprite::draw();
}

void CSpriteBow::SetLinePara(cocos2d::CCPoint ptUp, cocos2d::CCPoint ptDown)
{
    m_ptUp = ptUp;
    m_ptDown = ptDown;
    m_ptCenter = ccp((ptUp.x+ptDown.x)*0.5f, (ptUp.y+ptDown.y)*0.5f);
}


void CSpriteBow::TestBack()
{
    if(m_bTouched)
    {
        return;
    }
    
    cocos2d::CCPoint ptOrgCenter = ccp(0.5f * (m_ptUp.x + m_ptDown.x), 0.5f * (m_ptUp.y + m_ptDown.y));
    float len = ccpDistance(m_ptCenter, ptOrgCenter);
    
    if(abs(len) < BOW_BACK_SPEED)
    {
        m_ptCenter = ptOrgCenter;
        return;
    }
    
    cocos2d::CCPoint ptInc;
    float length = ccpDistance(m_ptCenter, ptOrgCenter);
    if(abs(length)< 0.0001)
    {
        ptInc = cocos2d::CCPoint(0, 0);
    }
    else
    {	
        ptInc = cocos2d::CCPoint((len/3) * (ptOrgCenter.x - m_ptCenter.x) / length, (len/3) * (ptOrgCenter.y - m_ptCenter.y) / length);
    }
    
    
    m_ptCenter = ccpAdd(m_ptCenter, ptInc);
    
}


const BowInfo &CBowManager::GetBowInfo(int id)
{
    static BowInfo retInfo;
    switch (id) {
        case 1:
            retInfo._ptSize = ccp(171, 66);
            retInfo._ptAnchor = ccp(87, 53);
            retInfo._ptRight = ccp(134, 46);
            retInfo._ptLeft  = ccp(40, 46);
            strcpy(retInfo._name, "bow1.png");
            break;
        case 2:
            retInfo._ptSize = ccp(102, 32);
            retInfo._ptAnchor = ccp(51, 28);
            retInfo._ptRight = ccp(87, 23);
            retInfo._ptLeft  = ccp(15, 23);
            strcpy(retInfo._name, "bow2.png");
        default:
            break;
    }

    return retInfo;
}




